package example.dawangba.com.z09315222_finalandroidhomework;

import android.Manifest;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.TabHost;
import android.widget.Toast;

public class TabHostDemo extends TabActivity {

    private String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_host_demo);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            //检查该权限是否已经获取
            int i = ContextCompat.checkSelfPermission(this, permissions[0]);
            // 权限是否已经 授权 GRANTED---授权  DENIED---拒绝
            if (i != PackageManager.PERMISSION_GRANTED){
                // 如果没有授予该权限，就去提示用户请求
                showDialogTipUserRequestPermission();
            }
        }

        TabHost tabHost=getTabHost();
        TabHost.TabSpec spec;
        spec=tabHost.newTabSpec("0");
        spec.setIndicator("帧动画");
        Intent intent;
        intent=new Intent(TabHostDemo.this,FrameActivity.class);
        spec.setContent(intent);
        tabHost.addTab(spec);

        spec=tabHost.newTabSpec("1");
        spec.setIndicator("播放音乐");
        Intent intent1;
        intent1=new Intent(TabHostDemo.this,PlayMusicActivity.class);
        spec.setContent(intent1);
        tabHost.addTab(spec);

        spec=tabHost.newTabSpec("3");
        spec.setIndicator("播放视频");
        Intent intent2;
        intent2=new Intent(TabHostDemo.this,PlayVideoActivity.class);
        spec.setContent(intent2);
        tabHost.addTab(spec);
    }

    // 提示用户该请求权限的弹出框
    private void showDialogTipUserRequestPermission(){
        new AlertDialog.Builder(this)
                .setTitle("没有SD卡读取权限!")
                .setMessage("没有SD卡读权限将造成程序异常")
                .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startRequestPermission();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setCancelable(false).show();
    }

    private void startRequestPermission(){
        ActivityCompat.requestPermissions(this,permissions,321);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 321){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
                    //判断用户是否点击了不再提醒
                    boolean b = shouldShowRequestPermissionRationale(permissions[0]);
                    if (!b){
                        //提示用户去设置界面手动开启权限
                        showDialogTipUserGoToAppSetting();
                    } else {
                        finish();
                    }
                } else {
                    Toast.makeText(this, "权限获取成功", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    private void showDialogTipUserGoToAppSetting(){
        new AlertDialog.Builder(this)
                .setTitle("存储权限不可用!")
                .setMessage("请在设置中立即开启!")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        Uri uri = Uri.fromParts("package",getPackageName(),null);
                        intent.setData(uri);

                        startActivityForResult(intent,123);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setCancelable(false).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 123){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                int i = ContextCompat.checkSelfPermission(this,permissions[0]);
                //权限是否已经授权：GRANTED--授权 DENIED--拒绝
                if (i != PackageManager.PERMISSION_GRANTED){
                    //如果没有授权
                    Toast.makeText(this, "请到设置界面允许授权", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
